package model

import (
	"time"
)

type Project struct {
	ProjectId int       `json:"project_id" gorm:"primary_key;column:project_id"`
	Name      string    `json:"name" binding:"required"`
	Desc      string    `json:"desc" binding:"required"`
	Ctime     time.Time `json:"ctime" gorm:"column:ctime" sql:"default:CURRENT_TIMESTAMP"`
}

func (*Project) TableName() string {
	return "project"
}

func SaveProject(project *Project) (int, error) {
	var err error
	if project.ProjectId != 0 {
		var currentProject Project
		DBIns.First(&currentProject, project.ProjectId)
		err = DBIns.Model(currentProject).Updates(Project{Name: project.Name, Desc: project.Desc}).Error
	} else {
		err = DBIns.Save(project).Error
	}

	if err != nil {
		return 0, err
	} else {
		return project.ProjectId, nil
	}
}

func GetProjects() []Project {
	var projects []Project
	DBIns.Find(&projects)

	return projects
}
